// 2013-8-1下午06:32:58  net.betou.trade.action.abs.TradeAct.java

package net.betou.trade.action.abs;

import static net.betou.common.page.SimplePage.cpn;
import static net.betou.common.page.SimplePage.cpnPageSize;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.util.TradeUtils;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Trade;
import net.betou.trade.main.manager.CategoryMng;
import net.betou.trade.main.manager.TradeMng;
import net.betou.trade.main.manager.UserGroupMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;
import net.betou.trade.web.WebErrors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

/**
 * 
 * @author 叶装装
 */
public abstract class TradeAct < T > extends ActionSupport< T >{

	public String toList( Boolean openTrade, Integer pageSize, Integer pageNo, String filterStatus, String searchProperty, String searchValue, String orderProperty, String orderDirection, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		
		pageSize = cpnPageSize( pageSize );
		pageNo = cpn( pageNo );
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		Pagination pagination;
		
		if( openTrade ){
			pagination = manager.getOpenTradePage( admin, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, filterStatus );
		}else{
			pagination = manager.getCloseTradePage( admin, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, filterStatus );
		}
		
		model.addAttribute( "groupList", groupMng.getAllowReadList( null, null, null, null ) );
		model.addAttribute( "admin", admin );	
		paramToModel( model, pagination, null, pageSize, searchProperty, searchValue, orderProperty, orderDirection, filterStatus );
		return getListPage( );
	}
	
	protected String toEdit( HttpServletRequest request, Integer id, ModelMap model ){
		WebErrors errors = validateUpdate( request, id );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		Trade bean = manager.findById( id );
		
		model.addAttribute( "bean", bean );
		model.addAttribute( "groupList", groupMng.getAllowReadList( null, null, null, null ) );
		
		return getUpdatePage( );
	}
	
	protected WebErrors validateOUpdate( Integer id, Boolean system, Integer selfId, HttpServletRequest request ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		Trade trade = manager.findMyTrade( id, selfId, system );
		
		if( trade == null ){
			errors.addError( system ? Messages.DATA_NOT_EXIST : Messages.DATA_NOT_OPERATING);
			return errors;
		}
		
		return errors;
	}
	
	private WebErrors validateUpdate( HttpServletRequest request, Integer id ){
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id == 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		Trade trade = manager.findById( id );
		if( trade == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		return errors;
	}
	
	protected void toDelete( boolean open, Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		
		ResponseUtil util = validateDelete( ids, admin.getSystem( ), admin.getId( ), response, request );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}

		Trade [ ] beans = manager.deleteByIds( ids );

		for ( Trade bean : beans ){
			logMng.operating( 2, request, open ? Messages.LOG_OPEN_TRADE : Messages.LOG_CLOSE_TRADE, "id=" + bean.getId( ) );
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );
		return;
	}
	
	private ResponseUtil validateDelete( Integer [ ] ids, Boolean system, Integer adminId, HttpServletResponse response, HttpServletRequest request ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}
		for ( Integer id : ids ){
			Trade trade = manager.findMyTrade( id, adminId, system );
			if( trade == null ){
				util.addErrorResponse( system ? Messages.getNotExistMessage( id ) : Messages.DATA_NOT_OPERATING ); // 只要有一个数据不存在就提示不可删除
				break;
			}
		}
		return util;
	}
	
	
	@Autowired
	protected TradeMng manager;
	
	@Autowired
	protected UserGroupMng groupMng;
	
	@Autowired
	protected CategoryMng categoryMng;
}
